<template>
	<view class="index-container">
		<!-- 欢迎区域 -->
		<view class="welcome-section">
			<text class="welcome-title">欢迎使用 Asurada</text>
			<text class="welcome-subtitle" v-if="isLoggedIn">你好，{{ userInfo.username }}！</text>
			<text class="welcome-subtitle" v-else>请先登录以获得完整体验</text>
		</view>

		<!-- 功能卡片区域 -->
		<view class="feature-section">
			<view class="section-title">
				<text>主要功能</text>
			</view>
			
			<view class="feature-grid">
				<view class="feature-card" @click="goToChat">
					<view class="feature-icon">
						<text>💬</text>
					</view>
					<view class="feature-info">
						<text class="feature-name">智能对话</text>
						<text class="feature-desc">AI助手聊天功能</text>
					</view>
				</view>
				
				<view class="feature-card" @click="goToProfile">
					<view class="feature-icon">
						<text>👤</text>
					</view>
					<view class="feature-info">
						<text class="feature-name">个人中心</text>
						<text class="feature-desc">管理个人信息</text>
					</view>
				</view>
				
				<view class="feature-card" @click="goToSettings">
					<view class="feature-icon">
						<text>⚙️</text>
					</view>
					<view class="feature-info">
						<text class="feature-name">设置</text>
						<text class="feature-desc">应用设置选项</text>
					</view>
				</view>
				
				<view class="feature-card" @click="goToHelp">
					<view class="feature-icon">
						<text>❓</text>
					</view>
					<view class="feature-info">
						<text class="feature-name">帮助</text>
						<text class="feature-desc">使用帮助文档</text>
					</view>
				</view>
			</view>
		</view>

		<!-- 登录状态区域 -->
		<view class="status-section" v-if="!isLoggedIn">
			<view class="login-prompt">
				<text class="prompt-text">登录后可享受更多功能</text>
				<view class="login-actions">
					<view class="action-button primary" @click="goToLogin">
						<text>立即登录</text>
					</view>
					<view class="action-button secondary" @click="goToRegister">
						<text>注册账号</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import authManager from '@/utils/auth.js'

export default {
	data() {
		return {
			userInfo: {},
			isLoggedIn: false
		}
	},
	
	onShow() {
		// 每次显示页面时检查登录状态
		this.checkAuthStatus()
	},
	
	onLoad() {
		// 监听登录成功和登出成功事件
		uni.$on('userLoginSuccess', this.handleLoginSuccess)
		uni.$on('userLogoutSuccess', this.handleLogoutSuccess)
	},
	
	onUnload() {
		// 移除事件监听
		uni.$off('userLoginSuccess', this.handleLoginSuccess)
		uni.$off('userLogoutSuccess', this.handleLogoutSuccess)
	},
	
	methods: {
		/**
		 * 检查认证状态
		 */
		checkAuthStatus() {
			this.isLoggedIn = authManager.isAuthenticated()
			// 不再在主界面获取用户详细信息
			this.userInfo = authManager.getCurrentUser() || {}
		},
		
		/**
		 * 刷新用户信息
		 */
		refreshUserInfo() {
			// 不再在主界面刷新用户信息
			return Promise.resolve()
		},
		
		/**
		 * 处理登录成功事件
		 */
		async handleLoginSuccess() {
			// 重新检查认证状态并刷新用户信息
			await this.checkAuthStatus()
		},
		
		/**
		 * 处理登出成功事件
		 */
		handleLogoutSuccess() {
			// 清除用户信息并更新登录状态
			this.isLoggedIn = false
			this.userInfo = {}
		},
		
		/**
		 * 跳转到登录页面
		 */
		goToLogin() {
			uni.navigateTo({
				url: '/pages/login/login'
			})
		},
		
		/**
		 * 跳转到注册页面
		 */
		goToRegister() {
			uni.navigateTo({
				url: '/pages/register/register'
			})
		},
		
		/**
		 * 跳转到个人中心
		 */
		goToProfile() {
			if (this.isLoggedIn) {
				uni.navigateTo({
					url: '/pages/user/user'
				})
			} else {
				this.goToLogin()
			}
		},
		
		/**
		 * 跳转到聊天页面
		 */
		goToChat() {
			if (this.isLoggedIn) {
				uni.navigateTo({
					url: '/pages/chat/chat'
				})
			} else {
				uni.showModal({
					title: '提示',
					content: '请先登录后使用聊天功能',
					confirmText: '去登录',
					success: (res) => {
						if (res.confirm) {
							this.goToLogin()
						}
					}
				})
			}
		},
		
		/**
		 * 跳转到设置页面
		 */
		goToSettings() {
			uni.showToast({
				title: '设置功能开发中',
				icon: 'none'
			})
		},
		
		/**
		 * 跳转到帮助页面
		 */
		goToHelp() {
			uni.showToast({
				title: '帮助功能开发中',
				icon: 'none'
			})
		}
	}
}
</script>

<style>
.index-container {
	min-height: 100vh;
	background-color: #1e2433;
	padding: 0 30rpx;
}

.welcome-section {
	text-align: center;
	padding: 80rpx 0 60rpx;
}

.welcome-title {
	font-size: 48rpx;
	color: #ffffff;
	font-weight: bold;
	display: block;
	margin-bottom: 20rpx;
}

.welcome-subtitle {
	font-size: 28rpx;
	color: #8a8a9a;
	display: block;
}

.feature-section {
	margin-bottom: 60rpx;
}

.section-title {
	font-size: 32rpx;
	color: #ffffff;
	font-weight: bold;
	margin-bottom: 30rpx;
}

.feature-grid {
	display: grid;
	grid-template-columns: 1fr 1fr;
	gap: 20rpx;
}

.feature-card {
	background-color: #2a304d;
	border-radius: 20rpx;
	padding: 30rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	text-align: center;
}

.feature-icon {
	font-size: 48rpx;
	margin-bottom: 20rpx;
}

.feature-info {
	display: flex;
	flex-direction: column;
	align-items: center;
}

.feature-name {
	font-size: 28rpx;
	color: #ffffff;
	font-weight: bold;
	margin-bottom: 10rpx;
}

.feature-desc {
	font-size: 24rpx;
	color: #8a8a9a;
}

.status-section {
	margin-bottom: 60rpx;
}

.login-prompt {
	background-color: #2a304d;
	border-radius: 20rpx;
	padding: 40rpx;
	text-align: center;
}

.prompt-text {
	font-size: 28rpx;
	color: #ffffff;
	display: block;
	margin-bottom: 30rpx;
}

.login-actions {
	display: flex;
	justify-content: center;
	gap: 20rpx;
}

.action-button {
	padding: 20rpx 40rpx;
	border-radius: 40rpx;
	font-size: 28rpx;
}

.action-button.primary {
	background-color: #4e5eff;
	color: #ffffff;
}

.action-button.secondary {
	background-color: transparent;
	color: #4e5eff;
	border: 2rpx solid #4e5eff;
}

.user-preview {
	background-color: #2a304d;
	border-radius: 20rpx;
	padding: 30rpx;
	margin-bottom: 60rpx;
	display: none;
}

.preview-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 20rpx;
}

.preview-title {
	font-size: 28rpx;
	color: #ffffff;
	font-weight: bold;
}

.view-more {
	font-size: 24rpx;
	color: #4e5eff;
}

.preview-content {
	display: flex;
	flex-direction: column;
	gap: 15rpx;
}

.preview-item {
	display: flex;
	align-items: center;
}

.preview-label {
	font-size: 26rpx;
	color: #8a8a9a;
	margin-right: 20rpx;
}

.preview-value {
	font-size: 26rpx;
	color: #ffffff;
}
</style>